using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using UnityEngine;

namespace BCW.UI.Animation
{
	public class UIAnimator : MonoBehaviour
	{
		public ExecuteOn executeOn = ExecuteOn.OnEnable;

		public UIButton button;

		public List<AnimatorSequenceUnit> sequence = new List<AnimatorSequenceUnit>();

		private Coroutine _sequencer;

		private void Awake()
		{
			HOTween.Init();
			if (button != null)
			{
				button.onClick.Add(new EventDelegate(delegate
				{
					Play();
				}));
			}
		}

		private void Start()
		{
			if (executeOn == ExecuteOn.Start)
			{
				Play();
			}
		}

		private void OnEnable()
		{
			if (executeOn == ExecuteOn.OnEnable)
			{
				Play();
			}
		}

		public void Play()
		{
			if (_sequencer != null)
			{
				Disable();
			}
			_sequencer = StartCoroutine(Sequencer());
		}

		private IEnumerator Sequencer()
		{
			foreach (AnimatorSequenceUnit unit in sequence)
			{
				foreach (TweenSettings settings in unit.tweenSettings)
				{
					TweenParms param = new TweenParms();
					param.Ease(settings.easeType);
					if (settings.easeType == EaseType.AnimationCurve)
					{
						param.Ease(settings.curve);
					}
					if (settings.loop)
					{
						param.Loops(settings.loopsCount, settings.loopType);
					}
					param.Delay(settings.offset);
					object tweenObject = settings.target;
					switch (settings.animationType)
					{
					case AnimType.Scale:
						param.Prop("localScale", settings.endVector);
						tweenObject = settings.target.transform;
						settings.startVector = settings.target.transform.localScale;
						break;
					case AnimType.Position:
						param.Prop("localPosition", settings.endVector);
						tweenObject = settings.target.transform;
						settings.startVector = settings.target.transform.localPosition;
						break;
					case AnimType.Rotation:
						param.Prop("localRotation", Quaternion.Euler(settings.endVector));
						tweenObject = settings.target.transform;
						settings.startVector = settings.target.transform.localRotation.eulerAngles;
						break;
					case AnimType.Color:
						tweenObject = settings.target;
						if (settings.target.GetComponent<UIWidget>() != null)
						{
							param.Prop("color", settings.endColor);
							tweenObject = settings.target.GetComponent<UIWidget>();
							settings.startColor = ((UIWidget)tweenObject).color;
						}
						break;
					case AnimType.Alpha:
						if (settings.target.GetComponent<UIRect>() != null)
						{
							param.Prop("alpha", settings.endFloat);
							tweenObject = settings.target.GetComponent<UIRect>();
							settings.startFloat = ((UIRect)tweenObject).alpha;
						}
						break;
					case AnimType.SwitchObject:
						settings.switchObjectCoroutine = StartCoroutine(SwitchObject(settings.target, settings.endBool, settings.offset));
						settings.startBool = settings.target.activeSelf;
						break;
					}
					param.UpdateType(UpdateType.TimeScaleIndependentUpdate);
					if (settings.animationType != AnimType.SwitchObject)
					{
						settings.tween = HOTween.To(tweenObject, settings.time, param);
					}
				}
				yield return new WaitForSeconds(unit.duration);
			}
		}

		private IEnumerator SwitchObject(GameObject obj, bool val, float delay)
		{
			yield return new WaitForSeconds(delay);
			if (obj != null)
			{
				obj.SetActive(val);
			}
		}

		private void OnDisable()
		{
			Disable();
		}

		private void Disable()
		{
			if (executeOn == ExecuteOn.Non || executeOn == ExecuteOn.Start)
			{
				return;
			}
			if (_sequencer != null)
			{
				StopCoroutine(_sequencer);
				_sequencer = null;
			}
			foreach (AnimatorSequenceUnit item in sequence)
			{
				foreach (TweenSettings tweenSetting in item.tweenSettings)
				{
					if (tweenSetting.tween != null)
					{
						HOTween.Kill(tweenSetting.tween);
					}
					switch (tweenSetting.animationType)
					{
					case AnimType.Scale:
						tweenSetting.target.transform.localScale = tweenSetting.startVector;
						break;
					case AnimType.Position:
						tweenSetting.target.transform.localPosition = tweenSetting.startVector;
						break;
					case AnimType.Rotation:
						tweenSetting.target.transform.localRotation = Quaternion.Euler(tweenSetting.startVector);
						break;
					case AnimType.Color:
						if (tweenSetting.target.GetComponent<UIWidget>() != null)
						{
							tweenSetting.target.GetComponent<UIWidget>().color = tweenSetting.startColor;
						}
						break;
					case AnimType.Alpha:
						if (tweenSetting.target.GetComponent<UIRect>() != null)
						{
							tweenSetting.target.GetComponent<UIRect>().alpha = tweenSetting.startFloat;
						}
						break;
					case AnimType.SwitchObject:
						if (tweenSetting.switchObjectCoroutine != null)
						{
							StopCoroutine(tweenSetting.switchObjectCoroutine);
						}
						if (tweenSetting.target != base.gameObject)
						{
							tweenSetting.target.SetActive(tweenSetting.startBool);
						}
						break;
					}
				}
			}
		}
	}
}
